

<?php

class km_mdstruktur_model extends MY_Model {
	var $model = 'ADMIN_PRIMKOKAS.KM_MDSTRUKTUR';
	function getApp()
	{
		$ls_sql	=  "SELECT	MDSTRUKTUR_ID value,
							MDSTRUKTUR_NAMA display
					FROM	ADMIN_PRIMKOKAS.KM_MDSTRUKTUR
					WHERE MDSTRUKTUR_TYPE = 'DIV' or MDSTRUKTUR_TYPE = 'DEP' or MDSTRUKTUR_TYPE = 'PAR'
					ORDER BY MDSTRUKTUR_ID";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	function getDiv(){
		$ls_sql	=  "SELECT	MDSTRUKTUR_ID value,
							MDSTRUKTUR_NAMA display
					FROM	ADMIN_PRIMKOKAS.KM_MDSTRUKTUR
					WHERE MDSTRUKTUR_TYPE = 'DIV' 
					ORDER BY MDSTRUKTUR_TYPE, MDSTRUKTUR_NAMA";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}

	function getDep($id){
		$ls_sql	=  "SELECT	MDSTRUKTUR_ID value,
							MDSTRUKTUR_NAMA display
					FROM	ADMIN_PRIMKOKAS.KM_MDSTRUKTUR
					WHERE MDSTRUKTUR_TYPE = 'DEP' AND MDSTRUKTUR_PID = $id
					ORDER BY MDSTRUKTUR_TYPE, MDSTRUKTUR_NAMA";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return json_encode($result->result_array());
		}else{
			return json_encode(array());
		}
	}
	function getCls($id){
		$ls_sql	=  "SELECT	MDSTRUKTUR_ID value,
							MDSTRUKTUR_NAMA display
					FROM	ADMIN_PRIMKOKAS.KM_MDSTRUKTUR
					WHERE MDSTRUKTUR_TYPE = 'CLS' AND MDSTRUKTUR_PID = $id
					ORDER BY MDSTRUKTUR_TYPE, MDSTRUKTUR_NAMA";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return json_encode($result->result_array());
		}else{
			return json_encode(array());
		}
	}
	function getType($id){
		$ls_sql	=  "SELECT	MDSTRUKTUR_TYPE, MDSTRUKTUR_PATH
					FROM KM_MDSTRUKTUR
					WHERE MDSTRUKTUR_ID = $id";
					
		$result	= $this->db->query($ls_sql);
		$data = "";
		foreach($result->result_array() as $row){
			$type = $row['mdstruktur_type'];
			$md_path = $row['mdstruktur_path'];
		}
		if( $type == "DIV"){
			$data = "DEP";
			$md_path = $md_path;
		}elseif($type == "DEP") {
			$data = "CLS";
			$md_path = $md_path;
		}else{
			$data = "DIV";
			$md_path = "";
		}
		return json_encode(array('md_type' => $data, 'md_path' => $md_path));
	}
	function GetJSON()
	{
		//$ls_sql	= "	SELECT	mdstruktur_id, mdstruktur_pid, mdstruktur_kode, mdstruktur_nama, mdstruktur_type, mdstruktur_margin, mdstruktur_path
		$ls_sql	= "	SELECT  *		
					FROM	km_mdstruktur
					WHERE 	mdstruktur_nama != '(None)' and
					(UPPER(mdstruktur_kode) LIKE '%".strtoupper(post('keyword'))."%' OR 
							UPPER(mdstruktur_nama) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'mdstruktur_path' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert($this->model, $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('km_mdstruktur', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('km_mdstruktur', array('mdstruktur_kode' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('km_mdstruktur', array('mdstruktur_kode' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM km_mdstruktur a join km_barang b on a.mdstruktur_id=b.mdstruktur_id_div WHERE a.mdstruktur_kode = '".$arr_rule['mdstruktur_kode']."'"))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['mdstruktur_kode']."]<br />";
			}else{
				$this->db->delete('km_mdstruktur', array('mdstruktur_kode' => $arr_rule['mdstruktur_kode']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>